/**
 * Project: greenline-gateway-biz-manager
 * 
 * File Created at 2014年7月22日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.core.pushcenter.impl;

import java.util.List;

import javax.annotation.Resource;

import com.greenline.push.core.common.AbstractLocalCache;
import com.greenline.push.core.pushcenter.CacheCategoryManager;
import com.greenline.push.dal.pushcenter.CategoryDAO;
import com.greenline.push.dal.pushcenter.dataobject.CategoryDO;


/**
 * Type: CacheCategoryManagerImpl <br>
 * Date: 2014年7月22日 <br>
 * Desc:
 * 
 * @author zhaoli
 * @version V1.0
 */
public class CacheCategoryManagerImpl extends
		AbstractLocalCache<String, CategoryDO> implements CacheCategoryManager {
	@Resource
	private CategoryDAO categoryDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.greenline.gateway.biz.manager.Category.CacheCategoryManager#findByCache
	 * (com.greenline.gateway.biz.dal.Category.enums.AppType, java.lang.String)
	 */
	@Override
	public CategoryDO findByCache(String key) {
		return super.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.greenline.gateway.biz.manager.common.AbstractLocalCache#getkey(java
	 * .lang.Object)
	 */
	@Override
	protected String getkey(CategoryDO t) {
		return t.getCategoryName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.greenline.pay.biz.common.AbstractLocalCache#load()
	 */
	@Override
	protected void load() {
		lock.writeLock().lock();
		try {
			List<CategoryDO> list = this.listData();
			data.clear();
			if (list != null) {
				for (CategoryDO da : list) {
					data.put(getkey(da), da);
				}
			}
		} finally {
			updateTime = System.nanoTime();
			lock.writeLock().unlock();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.greenline.gateway.biz.manager.common.AbstractLocalCache#listData()
	 */
	@Override
	protected List<CategoryDO> listData() {
		return categoryDAO.listAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.greenline.gateway.biz.manager.Category.CacheCategoryManager#reloadCache
	 * ()
	 */
	@Override
	public boolean reloadCache() {
		load();
		return true;
	}

	@Override
	protected CategoryDO getDatebase(String k) {
		CategoryDO category = categoryDAO.getBycategoryName(k);
		if (null != category) {
			this.reloadCache();
		}
		return category;
	}

	@Override
	public List<CategoryDO> listCacheCategory() {
		return this.listCacheData();
	}

}
